import * as signalR from '@aspnet/signalr'
import store from '../store/index'
import router from '../router'
// import { resolve } from 'core-js/fn/promise'
export default {
  SR: {},
  //初始化连接
  connectServer: function(userId) {
    console.log(userId)
    var $this = this
    var conn = $.hubConnection('https://www.jinpin.love:8088/signalr')
    $this.proxy = conn.createHubProxy('chat')
    $this.getMsg()
    conn
      .start()
      .done(data => {
        $this.proxy.invoke('bindUserConnected', userId).done(() => {
          console.log('bindUserConnected')
          console.log('signalr连接成功')
        })

        //心跳匹配
        $this.proxy.invoke('Keeplive', userId).done(() => {
          console.log('Keeplive')
        })
        $this.proxy.on('keeplive', data => {
          // console.log(data)
        })

        // 获取在线用户列表
        // $this.proxy.on('onLineUsers', data => {
        //   console.log('onLineUsers')
        //   store.dispatch('setChatUserList', data)
        //   localStorage.setItem('onLineUsers', data)
        // })

        // 获取通话的消息列表
        $this.proxy.invoke('chatUserList', userId).done(() => {
          console.log('chatUserListINVOKE')
        })
        $this.proxy.on('chatUserList', data => {
          store.dispatch("setChatUserList", data);
          console.log(data)
          localStorage.setItem('chatUserList', data)
        })

        // 获取客服列表
        $this.proxy.invoke('GetManualServiceList').done(() => {})
        $this.proxy.on('GetManualServiceList', data => {
          console.log(data)
          localStorage.setItem('GetManualServiceList', data)
        })

        // 获取第三方客服列表
        $this.proxy.invoke('GetThreeManualServiceList').done(() => {})
        $this.proxy.on('GetThreeManualServiceList', data => {
          localStorage.setItem('GetThreeManualServiceList', data)
        })
      })
      .fail(data => {
        console.log(data)
      })
  },

  // 获取聊天记录
  getMessage(items) {
    var $this = this
    let userInfo = JSON.parse(localStorage.getItem('userInfo')) || ''
    var toUserid = items.UserId //接受者id
    var UserId = userInfo.id //发送着id
    console.log(toUserid)
    console.log(UserId)
    $this.proxy.invoke('getMessage', toUserid, UserId, 1, 15).done(() => {
      console.log('获取聊天记录')
    })
    $this.proxy.on('getMessage', data => {
      console.log(data)
      store.dispatch('setMessage', data)
    })
  },
  // 发送消息
  sendMsg(dataMsg) {
    var $this = this
    $this.proxy.invoke('send', JSON.stringify(dataMsg)).done(() => {
      console.log('发送成功')
    })
    $this.getMsg()
    // $this.content = ''
    // console.log(content)
    // setTimeout(() => {
    //   $this.hint()
    //   $this.records.push({
    //     time: new Date().toLocaleTimeString(),
    //     content: content,
    //     type: 2
    //   })
    //   $this.scrollToBottom()
    // }, 800)
    // console.log($this.records)
  },
  // 获取服务端消息
  getMsg() {
    var $this = this
    $this.proxy.on('addMessage', data => {
      console.log('接收到服务端的消息')
      console.log(data)
      store.dispatch('setRecords', data)
      // let msgData = JSON.parse(data)
      // console.log(msgData)
      // setTimeout(() => {
      //   $this.records.push({
      //     time: new Date().toLocaleTimeString(),
      //     content: msgData.MessageContent,
      //     type: 1
      //   })
      // }, 800)
    })
  },
  // 停止连接（这个方法好像没啥用，先放着吧）
  stopSR: function() {
    let that = this
    async function stop() {
      try {
        await that.SR.stop()
        console.log('signaR退出成功')
      } catch (err) {}
    }
    stop()
  }
}
